<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use Illuminate\Http\Request;
use App\Models\Linkslistsstyles;
use App\Models\Linkslists;
use App\Models\Linkslistsminapps;
use App\Models\Linkslistshtmls;
use App\Models\Miniappsauthorization;
use App\Services\WechatopenService;
use App\Services\ThirdapiService;
use App\Services\MiniappService;
use App\Services\LinkstokenService;
use Log,Exception;

class LinksthirdController extends Controller
{
    public function linksListsDetial(Request $request)
    {
        if (empty($request->links_id) || empty($request->users_id)) return response()->json(["status"=>"error","msg"=>"参数错误,请联系客服！"]);
        $links_id = $request->links_id;
        try {
            $users_id = decrypt($request->users_id);
        } catch (Exception $e) {
            $users_id = "";
        }
        if (empty($users_id)) return response()->json(["status"=>"error","msg"=>"参数错误,请联系客服！"]);
        $links = Linkslists::getLinks($links_id);
        if (empty($links)) return response()->json(["status"=>"error","msg"=>"链接不存在,请核实链接是否正确！"]);
        if ($users_id != 1 && $links->users_id != $users_id) return response()->json(["status"=>"error","msg"=>"您无权访问本链接！"]);
        if ($links->disable == 2) response()->json(["status"=>"error","msg"=>"链接已被删除！"]);
        if ($links->end_time <= time()) response()->json(["status"=>"error","msg"=>"链接已过期！"]);
        if ($links->left_pv <= 0) response()->json(["status"=>"error","msg"=>"访问量不足,请升级会员"]);

        $linksStyles = Linkslistsstyles::getLinksListsStyles($links_id);
        if (empty($linksStyles)) $linksStyles = ["style"=>1,"logo"=>"","button_border"=>1,"button_background_color"=>"#06ae56","button_text_color"=>"#FFFFFF","botton_text"=>"点击立即前往微信","postion"=>1,"gif_show"=>1,"redirect_style"=>1,"redirect_url"=>""];
        $path = explode("?",$links->path);
        $query = count($path) > 1 ? $path[1] : "";
        if (in_array($links->platfrom_type, [1,2,3,6,8,9,11,12,13])) {
            $key = str_pad($links_id,16,"W",STR_PAD_RIGHT);
            $timestamp = strtotime("+5 hour");
            $iv = $timestamp."WLWLWL";
            $sign = md5("wl".$iv.$key.$iv."lw");
            if(strpos($query,'id=') !== false){
                $query = $query."&key=".$key."&timestamp=".$timestamp."&nsign=".$sign;
            }else{
                $query = $query."&key=".$key."&timestamp=".$timestamp."&nsign=".$sign."&id=".$links_id;
            }
        }
        $msg = in_array($links->platfrom_type, [1,13]) ? $links->msg : "";
        return response()->json(["status"=>"success","msg"=>"获取成功!","data"=>["links_id"=>$links_id,"title"=>$links->title,"describe"=>$links->describe,"appid"=>$links->appid,"path"=>$path[0],"query"=>$query,"miniapp_type"=>$links->miniapp_type,"platfrom_type"=>$links->platfrom_type,"type"=>$links->type,"original_id"=>$links->original_id,"wechat"=>$links->wechat,"msg"=>$msg,"styles"=>$linksStyles]]);
    }

    public function miniappAccessToken(Request $request)
    {
        if (empty($request->appid) || empty($request->users_id) || empty($request->miniapp_type)) return response()->json(["status"=>"error","msg"=>"参数错误,请联系客服！"]);
        try {
            $users_id = decrypt($request->users_id);
        } catch (Exception $e) {
            $users_id = "";
        }
        if (empty($users_id)) return response()->json(["status"=>"error","msg"=>"参数错误,请联系客服！"]);
        $expire_time = strtotime("+2 hour");
        $linksToken = LinkstokenService::forthAccessToken($request->appid,$request->miniapp_type);
        if ($linksToken["status"] == "error") return response()->json(["status"=>"error","msg"=>$linksToken["msg"]]);
        $accessToken =$linksToken["access_token"];
        return response()->json(["status"=>"success","msg"=>"获取成功","access_token"=>$accessToken,"expire_time"=>$expire_time]);
    }

    public function linksListsHtmls(Request $request)
    {
        if (empty($request->links_id) || empty($request->users_id)) return response()->json(["status"=>"error","msg"=>"参数错误,请联系客服！"]);
        $links_id = $request->links_id;
        try {
            $users_id = decrypt($request->users_id);
        } catch (Exception $e) {
            $users_id = "";
        }
        if (empty($users_id)) return response()->json(["status"=>"error","msg"=>"参数错误,请联系客服！"]);
        $listshtmls = Linkslistshtmls::getLinksListsHtmls($links_id);
        return response()->json(["status"=>"success","msg"=>"获取成功","data"=>["html"=>$listshtmls->html,"script"=>$listshtmls->script]]);
    }

    public function linksListsPageView(Request $request)
    {
        try {
            $users_id = decrypt($request->users_id);
        } catch (Exception $e) {
            $users_id = "";
        }
        if (empty($users_id)) return response()->json(["status"=>"error","msg"=>"参数错误,请联系客服！"]);
        Redis::hset('page_view_'.$request->today, $request->links_id, $request->pv);
        return response()->json(["status"=>"success","msg"=>"数据统计提交成功！"]);

    }
}
